/*!
 * @file Game.cpp
 * @author Rocco Martino
 */
/***************************************************************************
 *   Copyright (C) 2014 by Rocco Martino                                   *
 *   martinorocco@gmail.com                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License as        *
 *   published by the Free Software Foundation; either version 2.1 of the  *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

/* ======================================================================= */
/* ....................................................................... */
#include <ooGame/Game>

#include <osgODE/Notify>
#include <osgODE/ODEObject>
/* ....................................................................... */
/* ======================================================================= */




using namespace ooGame ;




/* ======================================================================= */
/* ....................................................................... */
Game::Game(void):
    m_state             ( 0 )
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
Game::Game(const Game& other, const osg::CopyOp& copyop):
    osgODE::ODECallback     ( other, copyop ),
    m_state                 ( other.m_state ),
    m_sensor_list           ( other.m_sensor_list ),
    m_controller_list       ( other.m_controller_list ),
    m_actuator_list         ( other.m_actuator_list )
{
    if( copyop.getCopyFlags() & osg::CopyOp::DEEP_COPY_CALLBACKS ) {

        m_sensor_list.clear() ;
        m_controller_list.clear() ;
        m_actuator_list.clear() ;

        for( unsigned int i=0; i<other.m_sensor_list.size(); i++ ) {
            m_sensor_list.push_back( osg::clone(other.m_sensor_list[i].get(), copyop) ) ;
        }

        for( unsigned int i=0; i<other.m_controller_list.size(); i++ ) {
            m_controller_list.push_back( osg::clone(other.m_controller_list[i].get(), copyop) ) ;
        }

        for( unsigned int i=0; i<other.m_actuator_list.size(); i++ ) {
            m_actuator_list.push_back( osg::clone(other.m_actuator_list[i].get(), copyop) ) ;
        }

    }
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
Game::~Game(void)
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
void
Game::operator()( osgODE::ODEObject* object )
{



    for(unsigned int i=0; i<m_controller_list.size(); i++) {
        m_controller_list[i]->reset() ;
    }


    for(unsigned int i=0; i<m_actuator_list.size(); i++) {
        m_actuator_list[i]->reset() ;
    }




    for(unsigned int i=0; i<m_sensor_list.size(); i++) {
        (*m_sensor_list[i]) ( object, this ) ;
    }


    for(unsigned int i=0; i<m_controller_list.size(); i++) {
        (*m_controller_list[i]) ( object, this ) ;
    }


    for(unsigned int i=0; i<m_actuator_list.size(); i++) {
        (*m_actuator_list[i]) ( object, this ) ;
    }



    traverse( object ) ;
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
Game*
Game::asGame(void)
{
    return this ;
}
/* ....................................................................... */
/* ======================================================================= */
